<?php

	class GetTableColumnCommand implements ICommand
	{
		public function execute($event)
		{
			$table = $event->data['table'];
			
			$buffer = EbModel::$defaultDataSource->execute("SELECT * FROM $table");
			
			$res = array();
			
			if (count($buffer) > 0)
			{
				$buffer = $buffer[0];
				
				foreach ($buffer AS $key=>$value)
				{
					$type = 'VARCHAR(32)';
					
					if (is_numeric($value))
					{
						$type = 'INT';
					}
					elseif (is_string($value))
					{
						if (strlen($value) < 32)
						{
							$type = 'VARCHAR(32)';
						}
						elseif (strlen($value) < 64)
						{
							$type = 'VARCHAR(64)';
						}
						elseif (strlen($value) < 128)
						{
							$type = 'VARCHAR(128)';
						}
						else
						{
							$type = 'TEXT';
						}
					}
					
					$res []= (object) array('name' => $key,'type' => $type);
				}
			}
			else
			{
				throw new Exception("The table $table must contain some data!");
			}
			
			return $res;
		}
	}